package com.zhuangjie.rider.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhaungjie.base.utils.PropertiesCopyUtils.PropertiesCopy;
import com.zhuangjie.base.entity.LocationInfo;
import com.zhuangjie.rider.entity.RiderWorkRegion;
import com.zhuangjie.rider.helper.RiderModuleTokenDataProvider;
import com.zhuangjie.rider.mapper.RiderWorkRegionMapper;
import com.zhuangjie.rider.service.RiderWorkRegionService;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhuangjie
 * @since 2023-12-26
 */
@Service
public class RiderWorkRegionServiceImpl extends ServiceImpl<RiderWorkRegionMapper, RiderWorkRegion> implements RiderWorkRegionService {

    @Override
    public RiderWorkRegion getRiderWorkRegion() {
        RiderWorkRegion riderWorkRegion = this.getOne(Wrappers.lambdaQuery(RiderWorkRegion.class)
                .eq(RiderWorkRegion::getRiderId, RiderModuleTokenDataProvider.getCurrentRiderId()));
        return riderWorkRegion;
    }

    @Override
    public void updateRiderWorkRegion(LocationInfo locationInfo) {
        // 更新的字段
        RiderWorkRegion riderWorkRegion = PropertiesCopy.type(LocationInfo.class, RiderWorkRegion.class)
                .produce(locationInfo);
        // 更新的条件，根据骑手的id来更新
        LambdaQueryWrapper<RiderWorkRegion> updateRiderWorkRegionWrapper = Wrappers.lambdaQuery(RiderWorkRegion.class)
                .eq(RiderWorkRegion::getRiderId, RiderModuleTokenDataProvider.getCurrentRiderId());
        if (!this.update(riderWorkRegion,updateRiderWorkRegionWrapper)) {
            throw  new RuntimeException("更新常驻区域失败！");
        }
    }
}
